package com.sleepingcat.ft_flutter.channel

import androidx.fragment.app.FragmentActivity
import com.cat.android.lib_base.base.RouterActivity
import com.cat.android.lib_ui.widget.CommonDialog
import com.idlefish.flutterboost.FlutterBoost
import com.sleepingcat.ft_flutter.fragment.MyFlutterFragment
import com.sleepingcat.ft_flutter.pigeons.BoostBridge
import com.sleepingcat.lib_common.const.Routes
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.plugins.FlutterPlugin

/**
 * @作者 志浩
 * @时间 2025/3/11 20:49
 * @描述 TODO
 */
class BoostChannel(engine: FlutterEngine) : FlutterPlugin, BoostBridge.BoostHostApi {
    private val TAG = "UserChannel"
    val flutterApi by lazy { BoostBridge.BoostFlutterApi(engine.dartExecutor) }

    override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        BoostBridge.BoostHostApi.setup(binding.binaryMessenger, this)
    }

    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        BoostBridge.BoostHostApi.setup(binding.binaryMessenger, null)
    }

    override fun showNativeTransparentDialog(flutterRoute: String) {
        val activity = FlutterBoost.instance().currentActivity()
        if (flutterRoute.isEmpty()) {
            if (activity is FragmentActivity) {
                CommonDialog(activity)
                    .requireTitle("测试")
                    .requireMessage("原生弹窗")
                    .show(activity.supportFragmentManager, null)
            }
        } else {
            if (activity is RouterActivity<*>) {
                activity
                    .navigate(
                        resId = Routes.SingleFlutterFragment.SINGLE_FLUTTER_FRAGMENT.hashCode(),
                        args = MyFlutterFragment.createArgument(
                            uniqueId = null,
                            url = flutterRoute,
                            requestCode = null,
                            urlParams = null,
                            opaque = false,
                        ),
                        optsBuilder = {
                            // 把进场动画给删了，不知道为啥有残影
                            it.setExitAnim(-1)
                            it.setEnterAnim(com.sleepingcat.lib_common.R.anim.alpha_in)
                            it.setPopExitAnim(com.sleepingcat.lib_common.R.anim.alpha_out)
                            it.setPopEnterAnim(-1)
                            it
                        }
                    )
            }
        }
    }
}